############################################
#### MUTUAL OPTIMIISM AS A CAUSE OF CONFLICT
#### BAS AND SCHUB
#### ISQ REPLICATION FILE
############################################

####################
## CONTENTS OVERVIEW
####################

# (I)  Figure 1 Left Panel: Histogram of secret alliances
# (II) Figure 1 Right Panel: Distribution of mutual optimism
# (III)Figure 2 Sensitivity to alternative priors
# (IV) Figure 3 Selection tests


##################
# (I) Figure 1 Left Panel: Histogram of secret alliances
##################
library(foreign)
rjs<-read.csv("SecretAllies_BasSchub.csv")

rjs$yr<-floor(rjs$begyr/10)*10
table(rjs$yr[rjs$offense==1])
table(rjs$yr[rjs$offense==0])
years<-seq(1810,1950,by=10)
off<-c(0,0,1,0,2,6,0,2,1,1,6,0,0,0,1)
def<-c(1,0,2,0,0,0,3,1,2,1,6,0,0,0,0)
data<-cbind(years,off,def,header=TRUE)
counts<-table(data[,2])
counts<-rbind(off,def)

par(mfrow=c(1,1))
barplot(counts,col=c("dodgerblue2","grey40"),names=years,yaxt="n",ylim=c(0,14),cex.names=1.2)
legend(bty="n","toplef",legend=c("defensive","offensive"),pch=c(19,19),col=c("grey40","dodgerblue2"),bg=c("blue","red"),cex=1.6)
mtext(side=2,"Secret Alliances Signed by Decade",outer=FALSE,line=2.5,cex=1.4)
axis(2,at=seq(0,14,by=2),cex.axis=1.2)
axis(1,at=seq(1810,1950,by=20),cex.axis=1.2)
mtext(side=3,"Secret Alliance Frequency",outer=FALSE,line=1,cex=1.5)


##################
# (II) Figure 1 Right Panel: Distribution of mutual optimism
##################
library(foreign)
rs<-read.dta("MOmain_BasSchub.dta")
rjs<-rs[rs$ongomid==0,]

dens<-density(rjs$msodpri)
dens$x[277]
pend<-length(dens$x)
px<-c(rev(dens$x[277:pend]),(dens$x[277:pend]))
py<-c(rep(0,length(277:pend)),dens$y[277:pend])

par(mfrow=c(1,1))
par(mar=c(2,2,2,2))
par(oma=c(2.5,2,2,1))
plot(density(rjs$msodpri,adjust=1),lwd=2,yaxt="n",ylab="",xlab="",xlim=c(0,2),yaxs="i",ylim=c(0,6.2),main="")
mtext(side=3,line=2,outer=FALSE,"Mutual Optimism",cex=1.4)
mtext(side=3,line=0.7,outer=FALSE,"Prevalence",cex=1.4)
abline(h=0,lty=1)
segments(1,0,1,1.2,lty=3,lwd=2)
polygon(px,py,col="firebrick")
mtext("Sum of Estimated Probabilities of Victory",side=1,line=2.4,outer=FALSE,cex=1.3)

##################
# (III)Figure 2 Sensitivity to alternative priors
##################

## Values in "PriorSensitivity_BasSchub.csv" calculated in
## "BasSchub_MainAnalysis.do" Section I

library(foreign)
rs<-read.csv("PriorSensitivity_BasSchub.csv",header=FALSE)
rjs<-as.data.frame(rs)
colnames(rjs)<-c("prior","ev","lo","hi")

par(mfrow=c(1,1))
par(mar=c(2,3,2,3))
par(oma=c(2,2,2,0))
plot(rjs$prior,rjs$ev,type="l",lwd=4,ylim=c(-0.42,3),xaxs="i",xaxt="n",yaxt="n")
points(rjs$prior,rjs$lo,type="l",lwd=3,lty=2,col="grey50")
points(rjs$prior,rjs$hi,type="l",lwd=3,lty=2,col="grey50")
abline(h=0,lty=3)
segments(rjs$prior[rjs$prior==2.8],rjs$lo[rjs$prior==2.8],rjs$prior[rjs$prior==2.8],rjs$hi[rjs$prior==2.8],col="firebrick",lwd=3)
axis(1,at=seq(0,4,by=0.5),cex.axis=1.3)
axis(2,at=seq(-0.5,3,by=0.5),cex.axis=1.3)
mtext(side=1,line=0.7,cex=1.3,"Prior Expectation of Secret Allies",outer=TRUE)
mtext(side=2,line=0.5,cex=1.3,"Percentage Point Increase in Conflict Probability",outer=TRUE)
mtext(side=3,line=0.2,cex=1.4,"Marginal Effect of Mutual Optimism:",outer=TRUE)
mtext(side=3,line=-1.2,cex=1.4,"Sensitivity Analysis Across Priors",outer=TRUE)

##################
# (IV)Figure 3 Selection tests
##################

## Values in "PriorSensitivity_BasSchub.csv" calculated in
## "BasSchub_MainAnalysis.do" Section I, II, and III

# 1) Base Model
base<-c(1.6,0.8,2.5)
# 2) Year Drop
yrdrop<-c(1.3,0.2,2.5)
# 3) Defensive Alliances
def<-c(1.2,0.2,2.3)
# 4) Drop Non-Alliance Dyads
noally<-c(1.2,0.4,2.1)
# 5) Targeted
targ<-c(1.0,0.0,2.0)
# 6) Non-Targeted
ntarg<-c(3.4,1.6,5.5)

all<-rbind(ntarg,targ,noally,def,yrdrop,base)
all<-as.data.frame(all)
colnames(all)<-c("ev","lo","hi")

par(mar=c(2,9,2,3))
par(oma=c(2,2,2,0))
plot(all$ev,1:6,xlim=c(-0.5,6),col="white",ylim=c(0.8,6.2),xaxt="n", yaxt="n",xlab="",ylab="")
segments(all$lo,1:6,all$hi,1:6,lwd=3,col="grey50")
points(all$ev,1:6,pch=20,xlim=c(-0.5,6),col="black",cex=1.5)
abline(v=0,lty=3,lwd=2)
mtext(side=1,line=2.2,cex=1.2,"Percentage Point Increase in Conflict Probability",outer=FALSE)
mtext(side=3,line=1.9,cex=1.4,"Marginal Effect of Mutual Optimism",outer=FALSE)
mtext(side=3,line=0.6,cex=1.4,"Across Robustness Test Specifications",outer=FALSE)
axis(1,at=seq(0,6,by=1),cex.axis=1.2)
axis(2,at=seq(1,6,by=1),cex.axis=1.2,c("Non-Target Specific","Target Specific","Alliance Dyads","Defensive Alliances","No Recent Signers","Base Specification"),las=1)




















